/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DB;

import java.sql.*;
import java.util.HashMap;

/**
 *
 * @author Sibrand
 */
public class DB {

    public Connection conn;

    public DB() {
        conn = null;
    }

    public DB(String dbName, String userName, String password) {
        conn = null;
        makeConnection(dbName, userName, password);
    }

    public DB(Connection c) {
        conn = c;
    }

    private void makeConnection(String dbName, String userName, String password) {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            conn = DriverManager.getConnection(
                    "jdbc:mysql://" + dbName, userName, password);
        } catch (Exception e) {
            conn = null;
            e.printStackTrace();
        }
    }

    public boolean isConnected() {
        try {
            return conn != null && !conn.isClosed();
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public HashMap<String, String>[] getRows(String table, String keyName, String keyValue) {
        if (isConnected() && !(table.contains(" ") || keyName.contains(" ") || keyValue.contains(" ") || table.isEmpty() || keyName.isEmpty() || keyValue.isEmpty())) {
            try {
                Statement statement = conn.createStatement();
                String query = "SELECT * FROM " + table + " WHERE " + keyName + "=\"" + keyValue + "\"";

                return convertToHashMap(statement.executeQuery(query));
            } catch (Exception ex) {
                return new HashMap[0];
            }
        } else {
            return new HashMap[0];
        }
    }

    public int getNumRows(String table) {
        if (isConnected() && !(table.contains(" ") || table.isEmpty())) {
            try {
                Statement statement = conn.createStatement();
                String query = "SELECT * FROM " + table;
                ResultSet rs = statement.executeQuery(query);

                int teller = 0;

                while (rs.next()) {
                    teller++;
                }
                return teller;
            } catch (Exception ex) {
                return -1;
            }
        } else {
            return -1;
        }
    }

    public String[] getAllValuesForKey(String table, String column) {
        if (isConnected() && !(table.contains(" ") || table.isEmpty() || column.contains(" ") || column.isEmpty())) {
            try {
                Statement statement = conn.createStatement();
                String query = "SELECT " + column + " FROM " + table;
                ResultSet rs = statement.executeQuery(query);

                if (rs.next()) {
                    rs.previous();
                    String[] result = new String[1];
                    int currentRow = 0;

                    while (rs.next()) {
                        result[currentRow] = rs.getString(1);
                        if (rs.next()) {
                            rs.previous();
                            currentRow++;
                            String[] temp = new String[currentRow + 1];
                            System.arraycopy(result, 0, temp, 0, result.length);
                            result = temp;
                        }
                    }
                    return result;
                } else {
                    return new String[0];
                }
            } catch (Exception ex) {
                return new String[0];
            }
        } else {
            return new String[0];
        }

    }

    private HashMap<String, String>[] convertToHashMap(ResultSet rs) throws SQLException {
        if (rs.next()) {
            rs.previous();

            HashMap<String, String>[] result = new HashMap[1];
            ResultSetMetaData metaData = rs.getMetaData();
            int columnCount = metaData.getColumnCount();
            int currentRow = 0;

            while (rs.next()) {
                for (int i = 1; i <=
                        columnCount; ++i) {
                    result[currentRow].put(metaData.getColumnName(i), rs.getString(i));
                }

                if (rs.next()) {
                    rs.previous();
                    currentRow++;

                    HashMap<String, String>[] temp = new HashMap[currentRow + 1];
                    System.arraycopy(result, 0, temp, 0, result.length);
                    result = temp;
                }
            }
            return result;
        } else {
            return new HashMap[0];
        }
    }

    public boolean usernameExists(String user) {
        if (isConnected() && user != null && !user.isEmpty()) {
            try {
                PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM Users WHERE username=?");
                pstmt.setString(1, user);
                ResultSet rs = pstmt.executeQuery();
                if (rs.next()) {
                    return true;
                } else {
                    return false;
                }
            } catch (SQLException ex) {
                return false;
            }
        } else {
            return false;
        }
    }
}
